#!/bin/bash
# 邮件系统资源监控脚本

echo "======================================"
echo "邮件系统资源使用情况"
echo "======================================"
echo ""

# 系统资源概览
echo "【系统资源概览】"
echo "CPU核心数: $(nproc)"
echo "内存信息:"
free -h | grep -E "Mem|Swap"
echo ""

# 当前资源限制
echo "【当前Shell资源限制】"
echo "文件描述符限制: $(ulimit -n)"
echo "进程数限制: $(ulimit -u)"
echo "内存限制: $(ulimit -m)"
echo ""

# Python进程信息
echo "【Python进程资源使用】"
ps aux | head -1
ps aux | grep -E "python.*mail|uvicorn" | grep -v grep
echo ""

# 详细进程信息
MAIN_PID=$(pgrep -f "start_server.py" | head -1)
if [ -n "$MAIN_PID" ]; then
    echo "【主进程详细信息】(PID: $MAIN_PID)"
    echo "进程资源限制:"
    cat /proc/$MAIN_PID/limits | grep -E "Max open files|Max processes|Max memory"
    echo ""
    echo "进程状态:"
    cat /proc/$MAIN_PID/status | grep -E "VmSize|VmRSS|Threads"
    echo ""
fi

# 端口监听状态
echo "【端口监听状态】"
netstat -tlnp | grep ":9999" || ss -tlnp | grep ":9999"
echo ""

# 系统负载
echo "【系统负载】"
uptime
echo ""

# 磁盘使用
echo "【磁盘使用情况】"
df -h /www/wwwroot/mail.com
echo ""

echo "======================================"
echo "监控完成 - $(date)"
echo "======================================"